import { createRouter, createWebHashHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'

const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomeView,
      meta: {
        showTabbar: true,
      },
    },
    {
      path: '/cate',
      name: 'cate',
      component: () => import('../views/CateView.vue'),
      meta: {
        showTabbar: true,
      },
    },
    {
      path: '/cart',
      name: 'cart',
      component: () => import('../views/CartView.vue'),
      meta: {
        showTabbar: true,
        requiredLogin: true,
      },
    },
    {
      path: '/my',
      name: 'my',
      component: () => import('../views/MyView.vue'),
      meta: {
        showTabbar: true,
      },
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/LoginView.vue'),
    },
    {
      path: '/reg',
      name: 'reg',
      component: () => import('../views/RegView.vue'),
    },
    {
      path: '/detail',
      name: 'detail',
      component: () => import('../views/DetailView.vue'),
    },
    {
      path: '/search',
      name: 'search',
      component: () => import('../views/SearchView.vue'),
    },
    {
      path: '/userinfo',
      name: 'userinfo',
      component: () => import('../views/UserInfoView.vue'),
    },
    {
      path: '/updateAvatar',
      name: 'updateAvatar',
      component: () => import('../views/UpdateAvatarView.vue'),
    },
    {
      path: '/city',
      name: 'city',
      component: () => import('../views/CityView.vue'),
    },
  ],
})
router.beforeEach((to, from) => {
  //要访问的页面 的路由配置 to
  if (to.meta.requiredLogin == true) {
    //需要的登录
    // 判断用户是否已经的登录了
    let token = localStorage.getItem('token')
    if (token == null) {
      //没有登录
      let flag = confirm('未登录，是否要去登录')
      if (flag === true) {
        //点击确定 (重定向到-强制跳转登录页)
        return { path: '/login' }
      } else {
        //点击了取消
        return false
      }
    }
  } else {
    //不需要的登录
  }
})

export default router
